package com.hucheng.common.utils;

import com.hucheng.common.exception.EduException;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-06-26
 * @Description: 类描述
 **/
public class CommonUtils {
    private static Random rand = null;

    private static SecureRandom secureRand = null;

    private static String localhost = "";

    static {
        secureRand = new SecureRandom();
        rand = new Random(secureRand.nextLong());
        localhost = WebUtils.getInstance().getLocalHost();
    }

    public static String getTraceInfo() {
        StringBuffer stringBuffer = new StringBuffer();
        StackTraceElement stackTraceElement = (new Throwable()).getStackTrace()[1];
        stringBuffer.append("(");
        stringBuffer.append("className:").append(
                stackTraceElement.getClassName());
        stringBuffer.append(";fieldName:").append(
                stackTraceElement.getFileName());
        stringBuffer.append(";methodName:").append(
                stackTraceElement.getMethodName());
        stringBuffer.append(";lineNumber:").append(
                stackTraceElement.getLineNumber());
        stringBuffer.append(")");
        return stringBuffer.toString();
    }

    public static String getTraceInfo(Throwable e) {
        StringBuffer stringBuffer = new StringBuffer();
        StackTraceElement stackTraceElement = e.getStackTrace()[0];
        stringBuffer.append("(");
        stringBuffer.append("className:").append(
                stackTraceElement.getClassName());
        stringBuffer.append(";fieldName:").append(
                stackTraceElement.getFileName());
        stringBuffer.append(";methodName:").append(
                stackTraceElement.getMethodName());
        stringBuffer.append(";lineNumber:").append(
                stackTraceElement.getLineNumber());
        stringBuffer.append(")");
        return stringBuffer.toString();
    }


    //获得随机数
    public static int getRandom(int accuracy) {
        return (int) (Math.random() * accuracy);
    }

    public static String getUUID() {

        return UUID.randomUUID().toString();
    }

    public static String getRandom() {

        String str = MD5Utils.md5(getUUID() + System.currentTimeMillis() + getRandom(999999999) + rand.nextLong()  + localhost);
        str = str.toLowerCase();
		/*String date = (new Date()).getTime()+"";
		String str =TimeUtils.getSysTimeLong().substring(2)+MD5Utils.md5(""+getRandom(999999999)).substring(8, 16)+date+(new Random()).nextInt(10);
		if(str.length()>32){
			str = str.substring(0,32);
		}*/
        return str;
    }

    public static String getRandom(String string) {

        String str = MD5Utils.md5(getUUID() + System.currentTimeMillis() + getRandom(999999999) + rand.nextLong()  + localhost +  string);
        str = str.toLowerCase();

        return str;
    }

    public static String encodeBase64String(String data) {
        return Base64Utils.encode(data.getBytes()).trim();
    }

    public static String hiddenMobile(String mobile){
        if(ValidatorUtils.isEmpty(mobile)||mobile.length()<11){
            return mobile;
        }
        return mobile.substring(0,3)+"******"+mobile.substring(9,11);
    }

    public static String hiddenNickName(String nickName){
        if(ValidatorUtils.isEmpty(nickName)){
            return nickName;
        }
        if(nickName.length()<7){
            return nickName.substring(0,1)+"****";
        }
        return nickName.substring(0,1)+"****"+nickName.substring(nickName.length()-1,nickName.length());
    }

    /**
     * 用于生成红包编号字符串
     */
    public static final String REWARD_NUMBER_RANDOM = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";

    /**
     * 获取红包编号
     * @return
     */
    public static String getRewardNo(){
        Random random = new Random();
        StringBuffer result = new StringBuffer("QTYD");
        for (int i = 0; i < 10; i++) {
            result.append(REWARD_NUMBER_RANDOM.charAt(random.nextInt(34)));
        }
        return result.toString();
    }

    /**
     * 生成订单号
     * @return
     */
    public static String getOrderIdByTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate = sdf.format(new Date());
        String result = "";
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            result += random.nextInt(10);
        }
        return newDate + result;
    }


    /**
     * 登录密码处理
     * @param password
     * @return
     */
    public static String loginPwd(String password){
        return MD5Utils.md5("@)!%" + password + ")!!$");
    }
    /**
     * 支付密码处理
     * @param password
     * @return
     */
    public static String payPwd(String password){
        return MD5Utils.md5("@)$!" + password + "^&*()");
    }

    /**
     * 短信验证码
     * @return
     */
    public static String smsCode(){
        String rand = "";
        while (rand.length()<6) {
            rand = rand+(new Random()).nextInt(10);
        }
        return rand;
    }

    public static String getDesKey(){
        String md5 = MD5Utils.md5((new Date()).getTime()+"");
        return md5.substring(8,16).toUpperCase();
    }

    public static String encodeCardId(String cardId) throws EduException{
        return DesUtils.encrypt(cardId, "QTYD@)!%");//qtyd2015
    }

    public static String decodeCardId(String cardId) throws EduException{
        return DesUtils.decrypt(cardId, "QTYD@)!%");
    }

    public static String birthday(String cardId){
        if (cardId.length()==18){
            return cardId.substring(6,10)+"-"+cardId.substring(10,12)+"-"+cardId.substring(12,14);
        }
        return "";
    }

    public static String encodeBankAccount(String bankAccount) throws EduException{
        return DesUtils.encrypt(bankAccount, "%!*^%#QT");
    }

    public static String decodeBankAccount(String bankAccount) throws EduException{
        return DesUtils.decrypt(bankAccount, "%!*^%#QT");
    }

    public static String encodeUserToken(String redisKey) throws EduException{
        return DesUtils.encrypt(redisKey, "!@)#!&#&");
    }

    public static String decodeUserToken(String redisKey) throws EduException{
        return DesUtils.decrypt(redisKey, "!@)#!&#&");
    }

    public static String encodeJDBC(String value) throws EduException{
        return DesUtils.encrypt(value, "wNjQ3ODg");
    }

    public static String round(double value,int digits){
        return String.format("%."+digits+"f", value);
		/*BigDecimal bg = new BigDecimal(value);
        double result =  bg.setScale(digits, BigDecimal.ROUND_HALF_UP).doubleValue();
        java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        nf.setMinimumFractionDigits(digits);
        return nf.format(result);*/
    }

    public static String roundDown(double value,int digits){
        BigDecimal bg = new BigDecimal(value);
        double result =  bg.setScale(digits, BigDecimal.ROUND_DOWN).doubleValue();
        java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        nf.setMinimumFractionDigits(digits);
        return nf.format(result);
    }

    public static double add(double v1,double v2){
        String tp = round(v1, 2);
        String tp2 = round(v2, 2);
        double result = Double.valueOf(tp)+Double.valueOf(tp2);
        return Double.valueOf(CommonUtils.round(result, 2));
    }

    public static double sub(double v1,double v2){
        String tp = round(v1, 2);
        String tp2 = round(v2, 2);
        double result = Double.valueOf(tp)-Double.valueOf(tp2);
        return Double.valueOf(CommonUtils.round(result, 2));
    }

    public static void main(String[] args) {
		/*String accessid = getRandom("123121321");
		System.out.println(accessid);
		String accesskey = encodeBase64String(getRandom(accessid));
		System.out.println(accesskey);*/
        System.out.println(smsCode());
        System.out.println(loginPwd("904e5a3e7d86fd702f25652e73f1e7473130d6180f13a450"));
    }
}
